var mongoose = require('mongoose')
var Schema = mongoose.Schema

const markSchema = Schema({
  user: {
    type: Schema.Types.ObjectId,
    required: true,
    ref: 'User'
  },
  book: {
    type: Schema.Types.ObjectId,
    required: true,
    ref: 'Book'
  },
  type: {
    type: String,
    required: true
  },
  color: {
    type: String,
    required: true
  },
  cfiRange: {
    type: String,
    required: true
  },
  markText: {
    type: String,
    default: ''
  },
  created_at: Date,
  updated_at: Date
})

markSchema.pre('save', function (next) {
  const mark = this
  if (!mark.isModified || !mark.isNew) {
    next()
  } else {
    const currentDate = new Date()
    mark.updated_at = currentDate
    if (!mark.created_at)
      mark.created_at = currentDate
    next()
  }
})

module.exports = mongoose.model('Mark', markSchema)
